package com.erdos.hyt.mall.service.impl;/**
 * @auth Liurong
 * @date 2017/4/1.
 */


import com.erdos.hyt.mall.mapper.MallGoodsMapper;
import com.erdos.hyt.mall.mapper.MallGoodsSkuMapper;
import com.erdos.hyt.mall.mapper.MallImgsMapper;
import com.erdos.hyt.mall.model.MallGoods;
import com.erdos.hyt.mall.model.MallGoodsSku;
import com.erdos.hyt.mall.model.MallImgs;
import com.erdos.hyt.mall.service.MallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
/**
 * 商城商品Service实现类
 *
 * @author liurong
 * @create 2017/4/1
 **/
@Service("mallGoodsService")
public class MallGoodsServiceImpl implements MallGoodsService {

    @Override
    public List<MallGoods> getMallGoodsIndex(MallGoods mallGoods) {

        return null;
    }
    @Autowired
    private MallGoodsMapper mallGoodsMapper;

    @Autowired
    private MallGoodsSkuMapper mallGoodsSkuMapper;

    @Autowired
    private MallImgsMapper mallImgsMapper;

    @Override
    public MallGoods getGoodsById(Integer goodsId) {
        MallGoods mallGoods=mallGoodsMapper.selectMallGoodsById(goodsId);
        //获取sku
        BigDecimal priceMin=new BigDecimal(0);
        BigDecimal priceMax=new BigDecimal(0);
        Integer totalSku=0;
        List<MallGoodsSku> skuList=mallGoodsSkuMapper.getMallGoodsSkuListByGoodsId(goodsId);
        if(skuList!=null&&skuList.size()>0){
            priceMin=skuList.get(0).getSalePrice();
            priceMax=skuList.get(skuList.size()-1).getSalePrice();

            for (int x=0;x<skuList.size();x++){
                MallGoodsSku sku= skuList.get(x);
                //记录总库存数
                totalSku+=sku.getNowInventory();
            }

        }
        mallGoods.setPriceShow("￥"+priceMin+"-￥"+priceMax);
        mallGoods.setTotalSkuShow(totalSku);
        mallGoods.setSkuList(skuList);
        List<MallImgs> imgsList=mallImgsMapper.getMallImgListByGoodsId(goodsId);
        mallGoods.setImgsList(imgsList);
        return mallGoods;
    }
}
